/**
 * Basic data structures by Cédric Tabin
 * for Compiler construction
 */

object DSEntryPoint { 

    def main():Unit={{
    		println(new StackTest().test());
    }}
}

class StackTest
{
	var stack:Stack;

	def test():String = {
	
		stack = new Stack();
		stack = stack.init(5);
		
		stack = stack.push(1);
		stack = stack.push(2);
		stack = stack.push(3);
		stack = stack.push(4);
		stack = stack.push(5);
		stack = stack.push(6);
	
		println("filled stack : "+stack.toString());
	
		println("popped : "+stack.pop());
		println("popped : "+stack.pop());
		println("popped : "+stack.pop());
	
		println("and now : "+stack.toString());
	
		return "";
	}
}

class Stack
{
	var stack:Int[];
	var size:Int;
	var index:Int;
	var retour:Int;
	var container:String;
	var loopIndex:Int;
	
	def init(s:Int):Stack =
	{
		stack = new Int[s];
		size = s;
		index = 0;
		
		return this;
	}
	
	def push(i:Int):Stack =
	{
		if (index < size) {	
			stack[index] = i;
			index = index+1;
		}
		else {
			println("Error : there is no more place (size="+size+")");
		}
		
		return this;
	}
	
	def pop():Int =
	{
		if (0 < index) {
			index = index-1;
			retour = stack[index];
		}
		else {
			println("Error : there is no element in the stack");
			retour = 0;
		}
		
		return retour;
	}
	
	def toString():String = {
		container = "";
		loopIndex = 0;
		
		while(loopIndex < index)
		{
			container = container+stack[loopIndex]+" ";
			loopIndex = loopIndex+1;
		}
		
		return container;
	}
}
